package com.wyw.mahjong.server.processor;

import com.wyw.mahjong.common.msg.*;
import com.wyw.mahjong.common.protocol.MahjongMessage;

/**
 * @Title   消息处理器
 * @Description 维护消息与处理器之间的映射，与前面消息与 cmd 的映射一样，我这里同样使用枚举的形式来实现
 *              对于 MahjongProcessor 我们维护的是一个一个的实例，而不再是 Class 类，因为 MahjongProcessor 是设计成无状态的，整个系统只需要一个实例，即单例。
 * @Author Mr Wu yewen.wu.china@gmail.com
 * @Date 2024/5/6 18:17
 * Update History:
 * Author        Time            Content
 */
public enum MahjongProcessorManager {
    HELLO_REQUEST_PROCESSOR(HelloRequest.class, new HelloRequestProcessor()),
    LOGIN_REQUEST_PROCESSOR(LoginRequest.class, new LoginRequestProcessor()),
    CREATE_ROOM_REQUEST_PROCESSOR(CreateRoomRequest.class, new CreateRoomRequestProcessor()),
    ENTER_ROOM_REQUEST_PROCESSOR(EnterRoomRequest.class, new EnterRoomRequestProcessor()),
    OPERATION_REQUEST_REQUEST_PROCESSOR(OperationRequest.class, new OperationRequestProcessor()),
    START_GAME_MESSAGE_PROCESSOR(StartGameMessage.class, new StartGameMessageProcessor()),
    ;

    private Class<? extends MahjongMessage> msgType;
    private MahjongProcessor mahjongProcessor;

    MahjongProcessorManager(Class<? extends MahjongMessage> msgType, MahjongProcessor mahjongProcessor) {
        this.msgType = msgType;
        this.mahjongProcessor = mahjongProcessor;
    }

    public static MahjongProcessor choose(MahjongMessage message) {
        for (MahjongProcessorManager value : MahjongProcessorManager.values()) {
            if (value.msgType == message.getClass()) {
                return value.mahjongProcessor;
            }
        }
        return null;
    }

}
